<template>
  <el-dialog title="请选择资讯" :visible.sync="dialogFormVisible" width="30%">
    <vab-query-form>
      <el-form
        ref="form"
        :model="queryForm.queryBean"
        :inline="true"
        @submit.native.prevent
      >
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.title"
            placeholder="标题"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-input
            v-model.trim="queryForm.queryBean.publisherName"
            placeholder="发布人"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            native-type="submit"
            @click="handleQuery"
          >
            查询
          </el-button>
        </el-form-item>
      </el-form>
    </vab-query-form>
    <el-table
      ref="tableSort"
      v-loading="listLoading"
      :data="list"
      :element-loading-text="elementLoadingText"
      :height="height"
    >
      <el-table-column
        show-overflow-tooltip
        label="标题"
        prop="title"
        width="100"
      ></el-table-column>
      <!-- subTitle -->
      <el-table-column
        show-overflow-tooltip
        label="子标题"
        prop="subTitle"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="发布人名称"
        prop="publisherName"
        width="100"
      ></el-table-column>
      <el-table-column
        show-overflow-tooltip
        label="发布人日期"
        prop="publishDate"
        width="100"
      ></el-table-column>
      <el-table-column show-overflow-tooltip label="选择">
        <template #default="{ row }">
          <el-button
            type="primary"
            icon="el-icon-connection"
            @click="handleSelect(row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    ></el-pagination>
  </el-dialog>
</template>

<script>
  import { getAllNews } from '@/api/new'
  import _ from 'lodash'
  export default {
    name: 'SelectNew',
    data() {
      return {
        tempData: {},
        list: [],
        listLoading: false,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        elementLoadingText: '正在加载...',
        dialogFormVisible: false,
        queryForm: {
          pageNum: 1,
          pageSize: 10,
          queryBean: {},
        },
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {},
    methods: {
      handleSelect(row) {
        let _data = {
          type: 'news',
        }
        _data.id = row.infoId
        _data.tempData = this.tempData
        _data.image = row.imageUrl
        this.$emit('setAd', _data)
        this.dialogFormVisible = false
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getAllNews(this.queryForm)
        const { body } = data
        this.list = body.data
        // data.forEach((item, index) => {})
        this.total = body.totalSize
        this.$baseNotify('数据获取完毕！', '成功', 'success', 'top-right')
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
      showEdit(_x) {
        this.fetchData()
        this.tempData = _x
        this.dialogFormVisible = true
      },
      close() {
        this.dialogFormVisible = false
      },
    },
  }
</script>
